package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.dto.SysQaDTO;
import com.fjwt.gz.db.entity.SysQaEntity;
import com.fjwt.gz.db.vo.SysQaVO;
import com.fjwt.gz.service.mapper.SysQaMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class SysQaService extends ServiceImpl<SysQaMapper, SysQaEntity> {

   @Autowired
   private SysQaMapper sysQaMapper;

    /**
     * pc端QA内容列表
     * @param iPage
     * @param sysQaDTO
     * @return
     */
    public IPage<SysQaVO> list(IPage iPage, SysQaDTO sysQaDTO) {
        return this.baseMapper.list(iPage, sysQaDTO);
    }

    /**
     * pc端新增QA内容
     * @param sysQaDTO
     */
    public void add(SysQaDTO sysQaDTO) {
        log.info("pc端新增QA内容{}", sysQaDTO);
        if (sysQaDTO.getSysQaTypeId() ==  null){
            throw new BizException("QA内容分类id不能为空");
        }
        if (sysQaDTO.getTitle().isEmpty()){
            throw new BizException("QA内容标题不能为空");
        }
        if (sysQaDTO.getContent().isEmpty()){
            throw new BizException("QA内容不能为空");
        }
        List<SysQaEntity> sysQaEntities = sysQaMapper.selectList(SysQaEntity.gw().
                eq(SysQaEntity::getSysQaTypeId, sysQaDTO.getSysQaTypeId())
                .eq(SysQaEntity::getTitle, sysQaDTO.getTitle())
                .eq(SysQaEntity::getIsDeleted, 0));
        if (!sysQaEntities.isEmpty()){
            throw new BizException("QA内容已存在,请重新添加");
        }
        SysQaEntity sysQaEntity = BeanUtil.copyProperties(sysQaDTO, SysQaEntity.class);
        save(sysQaEntity);
    }

    /**
     * pc端修改QA内容
     * @param sysQaDTO
     */
    public void update(SysQaDTO sysQaDTO) {
        log.info("pc端修改QA内容{}", sysQaDTO);
        SysQaEntity sysQaEntity = BeanUtil.copyProperties(sysQaDTO, SysQaEntity.class);
        if (sysQaEntity.getSysQaId() ==  null){
            throw new BizException("QA内容id不能为空");
        }
        if (sysQaEntity.getIsDeleted()==1){
            updateById(sysQaEntity);
            return;
        }
        if (sysQaEntity.getSysQaTypeId() ==  null){
            throw new BizException("QA内容分类id不能为空");
        }
        if (sysQaEntity.getTitle().isEmpty()){
            throw new BizException("QA内容标题不能为空");
        }
        if (sysQaEntity.getContent().isEmpty()){
            throw new BizException("QA内容不能为空");
        }
        List<SysQaEntity> sysQaEntities = sysQaMapper.selectList(SysQaEntity.gw()
                .ne(SysQaEntity::getSysQaId, sysQaDTO.getSysQaId())
                .eq(SysQaEntity::getSysQaTypeId, sysQaDTO.getSysQaTypeId())
                .eq(SysQaEntity::getTitle, sysQaDTO.getTitle())
                .eq(SysQaEntity::getIsDeleted, 0)
        );
        if (!sysQaEntities.isEmpty()){
            throw new BizException("QA内容已存在,请重新修改");
        }
        updateById(sysQaEntity);
    }


    /**
     * 小程序端QA内容列表
     * @return
     */
    public List<SysQaVO> appletList(String sysType) {
        return sysQaMapper.appletList(sysType);
    }
}
